import { All, Logger,ClassSerializerInterceptor, VersioningType } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { HttpAdapterHost, NestFactory,Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { ResponseInterceptor } from './core/response.interceptor';
import { AllExceptionsFilter } from './config/all.exception.filter';

async function bootstrap() {
  const logger = new Logger('bootstrap');
  const app = await NestFactory.create(AppModule,{
    cors: true,
    logger: ['log', 'error', 'warn', 'debug', 'verbose'],
  });
  // swagger
  const config = new DocumentBuilder()
    .setTitle('Nest example')
    .setDescription('The nest API description')
    .setVersion('1.0')
    .addBearerAuth()
    // .addTag('cats')
    .build();
  const documentFactory = () => SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('swagger', app, documentFactory);//http://localhost:3000/swagger
  // 多版本控制
  app.setGlobalPrefix('api');
  // app.enableVersioning({
  //   type: VersioningType.URI,
  //   defaultVersion:['1','2'] // 默认版本
  // });
  // 注册全局响应拦截器
  app.useGlobalInterceptors(new ResponseInterceptor());
  // 全局异常过滤器
  const HttpAdapter = app.get(HttpAdapterHost);
  app.useGlobalFilters(new AllExceptionsFilter(HttpAdapter))
  // 利用拦截器过滤输出数据的敏感字段
  app.useGlobalInterceptors(new ClassSerializerInterceptor(app.get(Reflector)));
  await app.listen(process.env.PORT ?? 3000);
  logger.log(`main.ts Application is running on: ${await app.getUrl()}`);
}
bootstrap();
